#!/bin/bash
function install_jdk() {
    echo "安装JDK" >>$logs_file
    jdkPath=$(whiptail --backtitle "Author: Elvis" --title "安装位置" --inputbox \
    "输入JDK安装位置" 10 60 "/usr/local" 3>&1 1>&2 2>&3)
    exitstatus=$?
    if [ "$exitstatus" != "0" ];then
        return 1
    fi
    jdkPath=${jdkPath:-/usr/local}
    jdkPath=`echo $jdkPath`
    jdkPath=${jdkPath%/}
    
    jdk_MainVer=
    while [[ ! $jdk_MainVer =~ ^[1-9]+[0-9]*$ ]]
    do
        jdk_MainVer=$(whiptail --backtitle "Author: Elvis" --title "安装版本" --inputbox \
        "输入JDK大版本号，如6，7，8，9，10，11" 10 60 "7" 3>&1 1>&2 2>&3)
        exitstatus=$?
        if [ "$exitstatus" != "0" ];then
           sleep 0.5
           return 1
        fi
        if [[ ! $jdk_MainVer =~ ^[1-9]+[0-9]*$ ]];then
           whiptail --backtitle "Author: Elvis" --title "警告" --msgbox "JDK大版本号必须为不以0开头的数字，请重新输入！" 10 70
           sleep 0.3
        fi
    done

    {
    echo 1
    sleep 1
    
    find $workHome/packages/jdk/* 2>/dev/null -maxdepth 0 -type d -exec rm -rf {} \;
    jdkPack=`ls -r $workHome/packages/jdk/ 2>/dev/null|grep -i "jdk-${jdk_MainVer}"|head -n1`
    
    if [ -z "${jdkPack}" ];then
        if [ "${die_network}" == "true" ];then
            msg="未找到任何安装包，且主机未联网。请确认packages/jdk/下有JDK${jdk_MainVer}安装包\n且安装包名称为jdk-<大版本u小版本>格式"
            echo $msg >>$logs_file
            echo -e "XXX\n10\n$msg\nXXX"
            echo 10
            sleep 1
            return 1
        else
            msg="下载安装包"
            echo $msg >>$logs_file
            echo -e "XXX\n40\n$msg\nXXX"
            echo 20
            
            main_download_url="https://sosnb-generic.pkg.coding.net/solo/jdk"
            case ${jdk_MainVer} in
            6)
                download_pkg="jdk-6u45-linux-x64.bin"
                ;;
            7)
                download_pkg="jdk-7u80-linux-x64.tar.gz"
                ;;
            8)
                download_pkg="jdk-8u162-linux-x64.tar.gz"
                ;;
            11)
                download_pkg="jdk-11.0.12_linux-x64_bin.tar.gz"
                ;;
            *)
                download_pkg="None"
                ;;
            esac
            
            echo "download_pkg: ${download_pkg}">>$logs_file
            
            if [ "${download_pkg}" != "None" ];then
                msg="下载JDK安装包中，此过程会持续5分钟以上，请等待......"
                echo $msg >>$logs_file
                echo -e "XXX\n22\n$msg\nXXX"
                echo 22
                curl --connect-timeout 30 -m 600 -fL --fail "${main_download_url}/${download_pkg}" -o $workHome/packages/jdk/${download_pkg} >>$logs_file 2>&1
                exit_status="$?"
                if [ "${exit_status}" != "0" ];then
                    msg="下载安装包失败"
                    echo $msg >>$logs_file
                    echo -e "XXX\n25\n$msg\nXXX"
                    echo 25
                    sleep 1
                    return 1
                fi
            else
                msg="不支持下载的版本，请手动下载安装包"
                echo $msg >>$logs_file
                echo -e "XXX\n25\n$msg\nXXX"
                echo 25
                sleep 1
                return 1
            fi     
        fi
    fi
    
    jdkPack=`ls -r $workHome/packages/jdk/ 2>/dev/null|grep -i "jdk-${jdk_MainVer}"|head -n1`
    
    jdkPack_=${jdkPack##*.}
    if [ "$jdkPack_" == "bin" ];then
        unPack_command="sh"
    elif [ "$jdkPack_" == "gz" ];then
        unPack_command="tar zxf"
    elif [ "$jdkPack_" == "bz2" ];then
        unPack_command="tar jxf"
    else
        msg="不支持的安装包格式，请使用bin、tar.gz、tar.bz2格式的二进制安装包\n可通过下面的网址去下载\nhttps://www.oracle.com/java/technologies/oracle-java-archive-downloads.html"
        echo -e $msg >>$logs_file
        echo -e "XXX\n30\n$msg\nXXX"
        echo 30
        sleep 1
        return 1
    fi
    
    msg="检查是否有openjdk并卸载"
    echo $msg >>$logs_file
    echo -e "XXX\n40\n$msg\nXXX"
    echo 40
    rpm -qa |grep openjdk>/dev/null 2>&1
    if [ "$?" == "0" ];then
        yum -y remove java-1.*-openjdk>/dev/null 2>&1
    fi
    
    msg="解压安装包"
    echo $msg >>$logs_file
    echo -e "XXX\n60\n$msg\nXXX"
    echo 60
    find $workHome/packages/jdk/* 2>/dev/null -maxdepth 0 -type d -exec rm -rf {} \;
    cd $workHome/packages/jdk/
    $unPack_command $jdkPack >>$logs_file 2>&1
    if [ "$?" != "0" ];then
        return 1
    fi
    unPack_name=`ls -F|grep "\/"`
    unPack_name=${unPack_name%/}
    
    echo "${jdkPath}/${unPack_name}" > $workHome/packages/jdk/installed_jdk_path
    
    msg="移动文件到安装位置"
    echo $msg >>$logs_file
    echo -e "XXX\n80\n$msg\nXXX"
    echo 80
    sleep 1
    if [ ! -d "$jdkPath" ];then
        mkdir -p $jdkPath >>$logs_file 2>&1
    fi
    [[ -n "$unPack_name" ]] && rm -rf $jdkPath/$unPack_name || return 1
    /bin/mv -f $unPack_name $jdkPath/ >>$logs_file 2>&1
    
    msg="配置环境变量"
    echo $msg >>$logs_file
    echo -e "XXX\n90\n$msg\nXXX"
    echo 90
    sleep 1
    sed -i "/JAVA_HOME=/d" /etc/profile
    #sed -i "/CLASSPAT=.:$JAVA_HOME/d" /etc/profile
    cat /etc/profile 2>/dev/null|grep 'PATH=$JAVA_HOME/bin.*:$JAVA_HOME/jre/bin.*' >/dev/null 2>&1
    exist_java_path="$?"
    cat /etc/profile 2>/dev/null|grep 'CLASSPAT=.*$JAVA_HOME/lib.*' >/dev/null 2>&1
    exist_java_clss="$?"
    
    class_line=`sed -n -e '/CLASSPAT\=.*$JAVA_HOME.*/=' /etc/profile|head -n1`
    path_line=`sed -n -e '/PATH\=.*$JAVA_HOME\/bin.*/=' /etc/profile|head -n1`
    if [ -n "$class_line" -a -n "$path_line" ];then
        if [ "$class_line" -lt "$path_line" ];then
          sed -i "${class_line}iexport JAVA_HOME=$jdkPath/$unPack_name" /etc/profile >/dev/null 2>&1
        else
          sed -i "${path_line}iexport JAVA_HOME=$jdkPath/$unPack_name" /etc/profile >/dev/null 2>&1
        fi
    elif [ -n "$class_line" -a -z "$path_line" ];then
        sed -i "${class_line}iexport JAVA_HOME=$jdkPath/$unPack_name" /etc/profile >/dev/null 2>&1
    elif [ -z "$class_line" -a -n "$path_line" ];then
        sed -i "${path_line}iexport JAVA_HOME=$jdkPath/$unPack_name" /etc/profile >/dev/null 2>&1
    else
        echo "export JAVA_HOME=$jdkPath/$unPack_name">>/etc/profile
    fi
    
    if [ "$exist_java_path" != "0" -a "$exist_java_clss" != "0" ];then
        echo "export CLASSPAT=.:\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib">>/etc/profile
        sed -i 's#:$JAVA_HOME/bin:#:#g' /etc/profile >/dev/null 2>&1
        sed -i 's#:$JAVA_HOME/jre/bin:#:#g' /etc/profile >/dev/null 2>&1
        sed -i 's#:$JAVA_HOME/bin/:#:#g' /etc/profile >/dev/null 2>&1
        sed -i 's#:$JAVA_HOME/jre/bin/:#:#g' /etc/profile >/dev/null 2>&1
        sed -i 's#:$JAVA_HOME/bin##g' /etc/profile >/dev/null 2>&1
        sed -i 's#:$JAVA_HOME/jre/bin##g' /etc/profile >/dev/null 2>&1
        sed -i 's#:$JAVA_HOME/bin/##g' /etc/profile >/dev/null 2>&1
        sed -i 's#:$JAVA_HOME/jre/bin/##g' /etc/profile >/dev/null 2>&1
        echo "export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH">>/etc/profile
    elif [ "$exist_java_path" != "0" -a "$exist_java_clss" == "0" ];then
        sed -i 's#:$JAVA_HOME/bin:#:#g' /etc/profile >/dev/null 2>&1
        sed -i 's#:$JAVA_HOME/jre/bin:#:#g' /etc/profile >/dev/null 2>&1
        sed -i 's#:$JAVA_HOME/bin/:#:#g' /etc/profile >/dev/null 2>&1
        sed -i 's#:$JAVA_HOME/jre/bin/:#:#g' /etc/profile >/dev/null 2>&1
        sed -i 's#:$JAVA_HOME/bin##g' /etc/profile >/dev/null 2>&1
        sed -i 's#:$JAVA_HOME/jre/bin##g' /etc/profile >/dev/null 2>&1
        sed -i 's#:$JAVA_HOME/bin/##g' /etc/profile >/dev/null 2>&1
        sed -i 's#:$JAVA_HOME/jre/bin/##g' /etc/profile >/dev/null 2>&1
        echo "export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH">>/etc/profile
    elif [ "$exist_java_path" == "0" -a "$exist_java_clss" != "0" ];then
        echo "export CLASSPAT=.:\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib">>/etc/profile
    fi
    #修改urandom
    if [ -f $jdkPath/$unPack_name/jre/lib/security/java.security ];then
        sed -i 's#^securerandom.source=file:/dev/urandom#securerandom.source=file:/dev/./urandom#g' $jdkPath/$unPack_name/jre/lib/security/java.security >/dev/null 2>&1
    fi

    source /etc/profile>/dev/null 
    
    echo -e "XXX\n100\ndone！！！\nXXX"
    echo 100
    sleep 0.15
    } | whiptail --backtitle "Author: Elvis" --title "安装JDK" --gauge " " 6 60 0
    
    sleep 0.15
    #判断是否安装成功
    source /etc/profile>/dev/null
    installed_bin_path=$(cat ${workHome}/packages/jdk/installed_jdk_path)/bin/java
    ${installed_bin_path} -version >>$logs_file 2>&1
    installd_jdkVer=$(${installed_bin_path} -version 2>&1|grep "java version"|awk '{print $3}'|sed 's/"//g')
    jdkVer=$(java -version 2>&1|grep "java version"|awk '{print $3}'|sed 's/"//g')
    
    echo "installd_jdkVer:${installd_jdkVer}" >>$logs_file
    echo "jdkVer: ${jdkVer}" >>$logs_file

    if [ "${installd_jdkVer}" == "${jdkVer}" ];then
        echo "JDK $jdkVer安装成功" >>$logs_file
        whiptail --backtitle "Author: Elvis" --title "消息" --msgbox "JDK $jdkVer安装成功" 10 70
    else
        msg="JDK $jdk_MainVer安装失败，请检查日志文件\n可能原因:\n1.下载安装包失败\n2.检查环境变量文件/etc/profile\n3.如果环境变量未生效，请重新登录尝试"
        echo -e "$msg" >>$logs_file
        whiptail --backtitle "Author: Elvis" --title "消息" --msgbox "$msg" 12 70
    fi
    sleep 0.30 
}